/**
 * @(#) AppmssProxy.as
 *
 * @date		2011. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	평가결과 Proxy - Proxy
 */
package com.skt.common_hr.empcont.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.utilSilver.textSilver;
	import com.skt.common_hr.empcont.model.vo.IfileVO;
	import com.skt.common_hr.empcont.model.vo.mainListVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.formatters.DateFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class EmpcontProxy extends CommonProxy
	{
		public static const NAME:String = "EmpcontProxy";

		public static const LOAD_MEDICALMONEY_LIST_COMPLETE:String	= "LoadMedicalMoneyListComplete";
		public static const LOAD_MEDICALMONEY_LIST_FAILED:String	= "LoadMedicalMoneyListFailed";


		
		public var mainList:ArrayCollection;
		public var mainListTop:ArrayCollection;
		public var mainListCode:ArrayCollection;
		public var mainListAttach:ArrayCollection;
		
		public var subTabList:ArrayCollection;
		public var acOitab:ArrayCollection;

		public var key:String;

		//지원기준
		public var loadFaultMessage:String;


		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function EmpcontProxy(data:Object=null)
		{
			super(NAME, data);
		}

		
		public function loadList(setFunc:Function):void
		{
			var obj:Object = {};
			obj.I_PERNR = Variables.myPernr;

			loadData("Z_HR_EC01", obj, loadListResult, listFault, setFunc);
		}
		
		public function loadListResult(event:ResultEvent):void
		{
			//objResult[key] = new ArrayCollection(objResult[key]);
			if(retCode==0) {							
				if(event.result.OTAB2 is ArrayCollection) {
					trace('OTAB2  ok');
				}else{
					trace('OTAB2  false');
//					Alert.show("OTAB2 : " + event.result.OTAB2.length);
					event.result.OTAB2 = new ArrayCollection(event.result.OTAB2);										
				}

				if(event.result.T_CODE is ArrayCollection) {
					trace('T_CODE  ok');
				}else{
					trace('T_CODE  false');
//					Alert.show("T_CODE : " + event.result.T_CODE.length);
					event.result.T_CODE = new ArrayCollection(event.result.T_CODE);
					
				}
								
				if(event.result.OTAB is ArrayCollection) {
					trace('OTAB  ok');
				}else{
					trace('OTAB  false');
//					Alert.show("OTAB : " + event.result.OTAB.length);
					event.result.OTAB = new ArrayCollection(event.result.OTAB);
					
				}
												
				mainListTop 	= parseTopList(event.result.OTAB2);				
				mainListCode 	= parseCodeList(event.result.T_CODE);
				mainList 		= parseMainList(event.result.OTAB);
				mainListAttach	= parseAttachList(event.result.OTAB1);
				
				var rtnObj:Object =  {};
				rtnObj.mainList 			= mainList;
				rtnObj.mainListTop 			= mainListTop;
				rtnObj.mainListCode 		= mainListCode;
				rtnObj.mainListAttach 		= mainListAttach;
				
				
				event.result.func(rtnObj);
			}
		}
		
		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}
		
		private function saveFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}
		
					
		private static var dateFormatter:DateFormatter = new DateFormatter();
		public static function toKorString(src:Date):String
		{
			dateFormatter.formatString = "YYYY년 MM월 DD일";
			return dateFormatter.format(src);
		}
			
		private function parseMainList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var rec:Object in recArr) 
			{
				var vo:mainListVO = new mainListVO();
				vo.ADDR			= rec["ADDR"];
				vo.BEGDA		= rec["BEGDA"];	
				
				vo.CDATE		= textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["CDATE"]))
				vo.CYES			= rec["CYES"];
				vo.ECODE		= rec["ECODE"];
				vo.ENAME		= rec["ENAME"];
				vo.ENDDA		= rec["ENDDA"];
				vo.ETEXT		= rec["ETEXT"];
				vo.PERNR		= textSilver.pernrFunc(rec["PERNR"]);
				vo.PYES			= rec["PYES"];
				vo.REGNO		= textSilver.regnoFunc(rec["REGNO"]);
				vo.SEQNR		= rec["SEQNR"];
				vo.PDATE		= rec["PDATE"];
				vo.CTIME		= rec["CTIME"];
				vo.ZYEAR		= rec["ZYEAR"]; 
				vo.JOINT		= textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["JOINT"]));
				vo.ORGTX		= rec["ORGTX"];
				vo.CTIME		= DateUtil.toTimeString(DateUtil.stringToDateTime(rec["CTIME"]));
				vo.CDATE_F 		= toKorString(DateUtil.stringToDateTime(rec["CDATE"]) );
				vo.CDATE 		= DateUtil.toDefaultString(DateUtil.stringToDateTime(rec["CDATE"]) );
				vo.ORGTX		= rec["ORGTX"];
				
				vo.CYES1		= rec["CYES1"];
				vo.CYES2		= rec["CYES2"];
				vo.CYES3		= rec["CYES3"];
				vo.CYES4		= rec["CYES4"]; 
				
				vo.CNO1		= rec["CNO1"];
				vo.CNO2		= rec["CNO2"];
				vo.CNO3		= rec["CNO3"];
				vo.CNO4		= rec["CNO4"];
				
				 
				
				
				if(vo.CDATE == '' ) {
					vo.CDATE_F2 = '';	
				} else {
					vo.CDATE_F2 	= DateUtil.toDefaultString(DateUtil.stringToDateTime(rec["CDATE"]) ) + ' ' + vo.CTIME;	
				}
				
				
				if(vo.CYES == "X") {
					vo.JOINT = textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["JOINT"]));
				}
				else {
					vo.JOINT = ""; 
				}
					
				
				vo.PDATE  		= DateUtil.toDefaultString(DateUtil.stringToDateTime(rec["PDATE"]) );
										
				if(vo.CYES == "X") {
					vo.CYES_F = "V";
				}
				else {
					vo.CYES_F = "";
				}
				
				if(vo.PYES == "X") {
					vo.PYES_F = "V";
				}
				else {
					vo.PYES_F = "";
				}
				
				
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		
		public static function toDotString(src:Date):String
		{
			dateFormatter.formatString = "YYYY년 MM월 DD일";
			return dateFormatter.format(src);
		}
				
		public function parseCodeList(recArr:ArrayCollection):ArrayCollection {
			return recArr;			
		}
		private function parseTopList(recArr:ArrayCollection):ArrayCollection
		{
			
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var rec:Object in recArr) 
			{
				var vo:mainListVO = new mainListVO();
				
				vo.ADDR			= rec["ADDR"];
				vo.BEGDA		= rec["BEGDA"];	
				vo.CDATE		= textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["CDATE"]));
				vo.CYES			= rec["CYES"];
				vo.ECODE		= rec["ECODE"];
				vo.ENAME		= rec["ENAME"];
				vo.ENDDA		= rec["ENDDA"];
				vo.ETEXT		= rec["ETEXT"];
				vo.PERNR		= textSilver.pernrFunc(rec["PERNR"]);
				vo.PYES			= rec["PYES"];
				vo.REGNO		= textSilver.regnoFunc(rec["REGNO"]);
				vo.SEQNR		= rec["SEQNR"];
				vo.PERIOD		= rec["PERIOD"];
				vo.PERSK		= rec["PERSK"];
				vo.PKTXT		= rec["PKTXT"];
				vo.PDATE		= rec["PDATE"];
				vo.CTIME		= rec["CTIME"];
				vo.ZYEAR		= rec["ZYEAR"];
				vo.JOINT		= textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["JOINT"]));
				vo.CTIME		= rec["CTIME"];
				vo.ORGTX		= rec["ORGTX"];
				
				vo.CBEGDA		= rec["CBEGDA"];
				vo.CENDDA		= rec["CENDDA"];
				
				//20140930
				vo.WBTXT = rec["WBTXT"];
				vo.SACHN = rec["SACHN"];
				vo.BAPAY = rec["BAPAY"];
				
				if(vo.CYES == 'X' ) {
					vo.CDATE 	= DateUtil.toDefaultString(DateUtil.stringToDateTime(rec["CDATE"]) ) + ' ' + DateUtil.toTimeString(DateUtil.stringToDateTime(rec["CTIME"]));	
				} else {
					vo.CDATE 	= '';
				}						
				
				if(vo.CYES == "X") {
					vo.JOINT = textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["JOINT"]));
				}
				else {
					vo.JOINT = ""; 
				}
					
									
				
				vo.CTIME		= DateUtil.toTimeString(DateUtil.stringToDateTime(rec["CTIME"]));
				vo.PDATE  		= DateUtil.toDefaultString(DateUtil.stringToDateTime(rec["PDATE"]) );
				vo.BEGDA_F		= toKorString(DateUtil.stringToDateTime(rec["CBEGDA"]) );
				vo.ENDDA_F		= toKorString(DateUtil.stringToDateTime(rec["CENDDA"]) );				
				vo.CDATE_F		= DateUtil.toDotString(DateUtil.stringToDateTime(rec.CDATE));
				
										
				if(vo.CYES == "X") {
					vo.CYES_F = "V";
				}
				else {
					vo.CYES_F = "";
				}
				
				if(vo.PYES == "X") {
					vo.PYES_F = "V";
				}
				else {
					vo.PYES_F = "";
				}
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		
		
		private function parseAttachList(recArr:ArrayCollection):ArrayCollection
		{
			
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var obj:Object in recArr){
				var retObj:IfileVO = new IfileVO();  

				for(var field:String in obj) {
					if(retObj.hasOwnProperty(field)==false ) {
						continue;
					}
									
					if(field == "UDATE") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field]);					
					}												
					else {						
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}
		
	}		
}